home *** CD-ROM | disk | FTP | other *** search
- Path: cs.vu.nl!sun4nl!ittpub!ittpub!nntp
- Newsgroups: comp.lang.c++
- Subject: Re: How to handle error in constructor
- Message-ID: <1996Feb2.170821.1768@ittpub>
- From: wil@ittpub.nl (Wil Evers)
- Date: 2 Feb 96 17:08:20 WET
- References: <4eo6n6$rbp@cnn.exu.ericsson.se>
- Distribution: world
- Nntp-Posting-Host: lintilla
-
- In article <4eo6n6$rbp@cnn.exu.ericsson.se> ebumow@ebu.ericsson.com
- (Mickey Williams 66753) writes:
- > In article 5EG@teslab.lab.oz.au, andrew@teslab.lab.oz.au (Andrew
- Phillips) writes:
-
- > >I'm converting and enhancing a simple program in C to C++. Without
- > >going into too much detail I have a class that represents a file on
- > >disk. The contructor opens the file, but what should it do if the file
- > >cannot be opened? The C program just calls fopen() tests the return
- > >value and if there's an error it prints a message and exits.
- >
- > >In C++ I thought to set a flag in the constructor and have a member
- > >function that tests the flag to see if the file was opened correctly.
- >
- > In practice, it is impossible to ensure that this works as intended.
- > Also, every user of this class must understand this error handling
- > mechanism. On the other hand, exceptions are part of the standard, so
- > throwing one of the standard exceptions should be much safer.
-
- There may be alternatives to throwing an exception here. Basically, disk
- file objects should behave reasonbly when the constructor couldn't open
- the requested file: for instance, instead of just assuming the fopen()
- succeeded, they could report failure when someone tries a read or write
- operation.
-
- > >This seems rather inelegant -- I guess exceptions would be the elegant
- > >way but seem like overkill for such a simple program.
- >
- > Elegant, schmelegant. It's the correct tool for the job.
- > [example deleted]
-
- Is it? Suppose, for instance, that I want to check for some parameter
- settings in an optional configuration file. Here, all I need to know is if
- there are any options to set. If there are none, I don't care why, so if
- the first read on the non-existing file simply returns EOF, I will be
- perfectly happy.
-
- If the disk file class further provides me with the option of telling me
- if the file could be opened - and, if not, why not, then I can decide for
- myself how sophisticated my error handling will be.
-
- The problem with exceptions is, that I'm *required* to handle them or else
- my program will die - even if failure to open a file is nothing to be
- upset about. A decent disk file class should at least give me the option
- to say 'no thanks, no exceptions for me this time.'
-
- IMHO exceptions are useful for handling major disasters, not for small
- inconveniences. Classes simply throwing an exception from their
- constructors because the class's programmer is too lazy to keep the
- object's state consistent in the face of such a failure are a nuisance at
- best, and could easily cause severe paranoia for the class's users.
-
- - Wil
-
-